package gmu.swe.project.tracker;

import gmu.swe.project.peer.PeerInfo;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;

public class TrackerUI {

	private static TrackerUI tUI = new TrackerUI();
	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JTextField txtInterval = null;
	private JLabel jLabel1 = null;
	private JTextField txtConfigFile = null;
	private JButton btnConnect = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextArea = null;
	private JTabbedPane jTabbedPane = null;
	private JScrollPane jScrollPane1 = null;
	private JTextArea jTextArea1 = null;
	
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(602, 397));
			jFrame.setTitle("Tracker");
			jFrame.setContentPane(getJContentPane());
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					System.exit(0);
				}
			});
		}
		return jFrame;
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.NORTH);
			jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Config File:");
			jLabel1.setSize(new Dimension(87, 18));
			jLabel1.setLocation(new Point(179, 6));
			jLabel = new JLabel();
			jLabel.setText("Interval:");
			jLabel.setSize(new Dimension(66, 18));
			jLabel.setLocation(new Point(11, 6));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(0, 35));
			jPanel.add(jLabel, null);
			jPanel.add(getTxtInterval(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getTxtConfigFile(), null);
			jPanel.add(getBtnConnect(), null);
		}
		return jPanel;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}
	
	private JTextField getTxtInterval() {
		if (txtInterval == null) {
			txtInterval = new JTextField();
			txtInterval.setPreferredSize(new Dimension(90, 20));
			txtInterval.setSize(new Dimension(87, 20));
			txtInterval.setText("20");
			txtInterval.setLocation(new Point(85, 6));
		}
		return txtInterval;
	}

	private JTextField getTxtConfigFile() {
		if (txtConfigFile == null) {
			txtConfigFile = new JTextField();
			txtConfigFile.setLocation(new Point(275, 6));
			txtConfigFile.setText("C:\\Temp\\config.xml");
			txtConfigFile.setSize(new Dimension(198, 20));
		}
		return txtConfigFile;
	}

	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton();
			btnConnect.setText("Connect");
			btnConnect.setSize(new Dimension(93, 18));
			btnConnect.setLocation(new Point(484, 6));
			btnConnect.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ConnectClicked();
				}
			});
		}
		return btnConnect;
	}
	
	private void ConnectClicked()
	{
		btnConnect.setEnabled(false);
		new Tracker(txtInterval.getText(), this, this.txtConfigFile.getText()).start();
	}

	public void print(String txt)
	{
		if(jTextArea != null)
		{
			jTextArea.append(txt + "\n");
		}
	}
	public void updateTable(ArrayList<PeerInfo> pInfo)
	{
		String outputTxt = "";
		for (PeerInfo pr : pInfo)
		{
			outputTxt += pr.getIpAddress() + " : " + Integer.toString(pr.getPort()) + "\n";
			for(String file : pr.getFiles())
			{
				outputTxt += "\t" + file + "\n";
			}
		}
		jTextArea1.setText(outputTxt);
	}
	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("Peers", null, getJScrollPane1(), null);
			jTabbedPane.addTab("Output", null, getJScrollPane(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getJTextArea1());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes jTextArea1	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea1() {
		if (jTextArea1 == null) {
			jTextArea1 = new JTextArea();
		}
		return jTextArea1;
	}

	public static void main(String[] args) {
		JFrame tracker = tUI.getJFrame();
		tracker.setVisible(true);

	}

}
